// 字符串流Demo
// Created by FanGccU on 2025/6/22.
//

#include <iostream>
#include <sstream>
using namespace std;

/**
 * istringstream 从字符串读取数据
 */
void readFunc() {
    // 输出给多个变量需要通过空格进行分隔
    const string value = "10 30";
    istringstream iss(value);

    int num1; // 10
    double num2; // 30

    iss >> num1 >> num2;
    cout << "num1:" << num1 << " num2:" << num2 << endl;
}

/**
 * ostringstream 将数据写入字符串
 */
void writeFunc() {
    ostringstream oss;
    int num1 = 50;
    int num2 = 65;
    oss << num1 << " " << num2;
    cout << "oss:" << oss.str() << endl;
}

void readAndWriteFunc() {
    string data = "50 80";
    stringstream ss(data);

    int a;
    double b;
    ss >> a >> b;
    cout << "a value :" << a << "\t b value :" << b << endl;

    ss.str(""); // 清空内容
    ss.clear(); // 清除状态，恢复为初始状态

    cout << "sss:" << ss.str() << endl;

    ss << "new data value: " << 99 << " " << 44;

    string newValue = ss.str();
    cout << "print " << newValue << endl;
}

int main(int argc, char *argv[]) {
    // readFunc();
    // writeFunc();
    readAndWriteFunc();
}
